package JAVA300.第十章IO;

import org.apache.commons.io.FileSystem;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.io.filefilter.*;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * commonsIO的学习
 *
 */
public class IO_Study17_CommonsIO {
    public static void main(String[] args) {

        copy();
    }

    public static void getFileSize(){

        //用于计算文件目录等大小
        long len = FileUtils.sizeOf(new File("IO_16MergeOut.java"));
        System.out.println(len);
        long len1 = FileUtils.sizeOf(new File("D:/Java300_Exp/src/JAVA300/第十章IO"));
        System.out.println(len1);

    }

    //列出子孙级
    public static void getSon(){
        Collection<File> files = FileUtils.listFiles(new File("D:/Java300_Exp/src/JAVA300/第十章IO"),
                EmptyFileFilter.NOT_EMPTY,null);//对空文件过滤
        for (File file:files){
            System.out.println(file.getAbsolutePath());
        }

        System.out.println("--------------------------------------");

        //进一步遍历子孙级
        Collection<File> files1 = FileUtils.listFiles(new File("D:/Java300_Exp/src/JAVA300/第十章IO"),
                EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE);//
        for (File file:files1){
            System.out.println(file.getAbsolutePath());
        }

        //遍历java
        Collection<File> files2 = FileUtils.listFiles(new File("D:/Java300_Exp/src/JAVA300/第十章IO"),
                new SuffixFileFilter("java"), DirectoryFileFilter.INSTANCE);//
        for (File file:files1){
            System.out.println(file.getAbsolutePath());
        }

        //需要多种类型
        Collection<File> files3 = FileUtils.listFiles(new File("D:/Java300_Exp/src/JAVA300/第十章IO"),
                FileFilterUtils.or(new SuffixFileFilter("java"),new SuffixFileFilter("txt")), DirectoryFileFilter.INSTANCE);//
        for (File file:files1){
            System.out.println(file.getAbsolutePath());
        }
    }

    //读取内容
    public static void  read(){
        try {
            //读成字符串
            String msg = FileUtils.readFileToString(new File("IOTest01.txt"),"UTF-8");
            System.out.println(msg);

            //读到字节数组
            byte[] content = FileUtils.readFileToByteArray(new File("IOTest01.txt"));
            System.out.println(content.length);

            //逐行读取
            List<String> lists = FileUtils.readLines(new File("IOTest01.txt"),"UTF-8");
            for (String list:lists){
                System.out.println(list);
            }

            System.out.println("--------------------");

            //利用迭代器
            LineIterator it = FileUtils.lineIterator(new File("IOTest01.txt"),"UTF-8");
            while (it.hasNext()){
                System.out.println(it.nextLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //写出内容
    public static void write(){

        try {
            FileUtils.write(new File("IO_17WriteOut.txt"),"测试FileUtils的写出\r\n","UTF-8",true);
            FileUtils.writeByteArrayToFile(new File("IO_17WriteOut.txt"),"测试FileUtils的写出\n".getBytes("UTF-8"),true );

            //容器内容写出
            List<String> content = new ArrayList<String>();
            content.add("m11");
            content.add("m22");
            content.add("m33");

            FileUtils.writeLines(new File("IO_17WriteOut.txt"),content,"----",true);//第三个参数表示；列表条目连接符


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //复制文件
    public static void copy(){

        try {
            //copy文件
            FileUtils.copyFile(new File("IOTest01.txt"),new File("IOTest01_17Copy.txt"));
            //copy文件到目录
            FileUtils.copyFileToDirectory(new File("testpic.png"),new File("lib"));
            //copy目录到目录 a作为b的子目录
            FileUtils.copyDirectoryToDirectory(new File("lib"),new File("lib_IO17Copy"));
            //copy内容到目录 a的内容给b
            FileUtils.copyDirectory(new File("lib"),new File("lib_IO17Copy"));
            //copy URL
            FileUtils.copyURLToFile(new URL("http://www.baidu.com"),new File("IO_17URLOut"));

            //利用IOUtil直接读URL
            String datas = IOUtils.toString(new URL("http://www.163.com"),"gbk");
            System.out.println(datas);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
